package gui;

import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;

import java.awt.GridLayout;
import java.awt.Button;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JLabel;

import fachlogik.Spieler;
import fachlogik.SpielerListe;
import java.awt.Component;

public class SpielerAdd extends JDialog {

	private static final long serialVersionUID = -368913938864496700L;

	private JTextField nachname;
	private JTextField vorname;
	private JTextField position;
	private JSpinner alter;
	private JSpinner tore;
	private JSpinner assists;
	private SpielerListe liste;

	public SpielerAdd(SpielerListe l) {
		setModal(true);

		liste = l;
		this.setSize(500, 500);
		getContentPane().setLayout(new GridLayout(7, 1));

		JLabel lblVorname = new JLabel("Vorname: ");
		lblVorname.setAlignmentY(Component.TOP_ALIGNMENT);
		getContentPane().add(lblVorname);

		vorname = new JTextField();
		getContentPane().add(vorname);

		JLabel lblNachname = new JLabel("Nachname: ");
		getContentPane().add(lblNachname);

		nachname = new JTextField();
		getContentPane().add(nachname);

		JLabel lblPosition = new JLabel("Position: ");
		getContentPane().add(lblPosition);

		position = new JTextField();
		getContentPane().add(position);

		JLabel lblAlter = new JLabel("Alter: ");
		getContentPane().add(lblAlter);

		alter = new JSpinner(new SpinnerNumberModel(0, 0, 100, 1));
		getContentPane().add(alter);

		JLabel lblTore = new JLabel("Tore: ");
		getContentPane().add(lblTore);

		tore = new JSpinner(new SpinnerNumberModel(0, 0, 100, 1));
		getContentPane().add(tore);

		JLabel lblAssists = new JLabel("Assists: ");
		getContentPane().add(lblAssists);

		assists = new JSpinner(new SpinnerNumberModel(0, 0, 100, 1));
		getContentPane().add(assists);

		Button button = new Button("Speichern");
		getContentPane().add(button);
		button.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				Spieler s;
				try {
					s = new Spieler();
					s.setNachname(nachname.getText());
					s.setVorname(vorname.getText());
					s.setPosition(position.getText());
					s.setAlter((int) alter.getValue());
					s.setTore((int) tore.getValue());
					s.setAssists((int) assists.getValue());

					liste.add(s);
					dispose();
				} catch (Exception ioe) {
					ioe.printStackTrace();
					JOptionPane.showMessageDialog(SpielerAdd.this,
							ioe.getMessage());
				}
			}
		});

		this.setVisible(true);
	}

}
